#ifndef DOWNLOADMANAGERTABLE_H
#define DOWNLOADMANAGERTABLE_H

#include <QTableView>
#include <QItemDelegate>
#include <QApplication>
#include "MainWindow.h"
#include "MyQSortFilterProxyModel.h"
#include "DataModel.h"
#include <QDebug>


class DownloadManagerTable : public QTableView
{
	Q_OBJECT

	MyQSortFilterProxyModel *proxyModel;
	DataModel *sourceModel;
	int runningDownloadsNumber;
public:
	explicit DownloadManagerTable(QWidget *parent = 0);
	void selectionChanged(const QItemSelection &selected,
						  const QItemSelection &deselected);
	void selectColumn(int column);
	QVariant getData(const QModelIndex &index);
	void startDownload(Download *download);
	void createDownload(QString downloadLink, QString folder, int priority);

signals:
	void newDownload(Download *download);
	void isDownloadSelected(bool selected);
	void isRunningDownloadSelected(bool selected);
	void isNewDownloadSelected(bool selected);
	
public slots:
	void removeSelectedDownload();
	void stopSelectedDownload();
	void startSelectedDownload();
	void downloadFinished();
	void tryToStartPendingDownloads();
	
};




class DownloaderViewDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	inline DownloaderViewDelegate(QObject *parent) : QItemDelegate(parent) {}

	inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
					  const QModelIndex &index ) const
	{
		if (index.column() == DataModel::ProgressColumn) {
			// Set up a QStyleOptionProgressBar to precisely mimic the
			// environment of a progress bar.
			QStyleOptionProgressBar progressBarOption;
			progressBarOption.state = QStyle::State_Enabled;
			progressBarOption.direction = QApplication::layoutDirection();
			progressBarOption.rect = option.rect;
			progressBarOption.fontMetrics = QApplication::fontMetrics();
			progressBarOption.minimum = 0;
			progressBarOption.maximum = 100;
			progressBarOption.textAlignment = Qt::AlignCenter;
			progressBarOption.textVisible = true;

			// Set the progress and text values of the style option.
			int progress = qobject_cast<DownloadManagerTable *>(parent())->getData(index).toInt();
			progressBarOption.progress = progress < 0 ? 0 : progress;
			progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);

			// Draw the progress bar onto the view.
			QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
		} else if (index.column() == DataModel::PriorityColumn) {
			int priority = qobject_cast<DownloadManagerTable *>(parent())->getData(index).toInt();
			QString iconFilename = QString(":/priority/") + QString::number(priority) + QString(".png");
			int length = qMin(32, qMin(option.rect.height(), option.rect.width()));
			int offsetX = 0;
			int offsetY = 0;
			if (option.rect.width() > length) {
				offsetX = (option.rect.width() - length) / 2;
			}
			if (option.rect.height() > length) {
				offsetY = (option.rect.height() - length) / 2;
			}
			QRect rect(option.rect.x() + offsetX, option.rect.y() + offsetY, length, length);
			QApplication::style()->drawItemPixmap(painter, rect, 0,
												  QPixmap(iconFilename).scaled(QSize(length, length)));
		} else {
			QItemDelegate::paint(painter, option, index);
			return;
		}
	}
};


#endif // DOWNLOADMANAGERTABLE_H
